﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using XinYi.CrookedShowMode;

namespace XinYi.CrookedShow.BLL
{
    public class OrderService : BaseService<Order>
    {
        /// <summary>
        /// 删除两表表OrderInfo,Order_Detail
        /// </summary>
        /// <param name="entity"></param>
        /// <param name="roleId"></param>
        /// <param name="moduleId"></param>
        /// <returns></returns>
        public bool Delete(Order orderInfo, List<Order_Detail> order_Detail)
        {
            //创建实例
            using (var db = new CrookedShowEntities())
            {
                using (var tran = db.Database.BeginTransaction())
                {
                    try
                    {
                        //删除第一张表
                        db.Entry(orderInfo).State = EntityState.Deleted;

                        //删除第二张表                       
                        for (int i = 0; i < order_Detail.Count; i++)
                        {
                            db.Entry(order_Detail[i]).State = EntityState.Deleted;
                        }
                        //保存
                        db.SaveChanges();
                        //提交
                        tran.Commit();
                    }
                    catch (Exception ex)
                    {
                        //回滚
                        tran.Rollback();
                        return false;
                    }
                }
            }
            return true;
        }
        /// <summary>
        /// 批量删除
        /// </summary>
        /// <param name="entity"></param>
        /// <param name="roleId"></param>
        /// <param name="moduleId"></param>
        /// <returns></returns>
        public bool BatchDeleteById(List<Order> orderInfo, List<Order_Detail> order_Detail)
        {
            //创建实例
            using (var db = new CrookedShowEntities())
            {
                using (var tran = db.Database.BeginTransaction())
                {
                    try
                    {
                        //删除第一张表
                        for (int i = 0; i < orderInfo.Count; i++)
                        {
                            var entity = db.Order.Find(orderInfo[i].Order_Id);
                            if (entity != null)
                            {
                                db.Order.Remove(entity);
                            }
                        }

                        //删除第二张表                       
                        for (int i = 0; i < order_Detail.Count; i++)
                        {
                            var entity = db.Order_Detail.Find(order_Detail[i].Order_Detail_Id);
                            if (entity != null)
                            {
                                db.Order_Detail.Remove(entity);
                            }
                        }
                        //保存
                        db.SaveChanges();
                        //提交
                        tran.Commit();
                    }
                    catch (Exception ex)
                    {
                        //回滚
                        tran.Rollback();
                        return false;
                    }
                }
            }
            return true;
        }
        
    }
}
